package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.example.dao.ordergoods.OrderGoodsMapper;
import org.example.dao.orderuser.OrderUserMapper;
import org.example.entity.*;
import org.example.service.OrderUserService;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderUserServiceImpl implements OrderUserService {

    @Resource
    private OrderUserMapper orderUserMapper;

    @Resource
    private OrderGoodsMapper orderGoodsMapper;
    /**
     * 添加新的订单
     * */
    @Override
    public boolean addOrderUser(OrderUser orderUser) {
        boolean flag = false;
        OrderGoods orderGoods;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月和小时的格式为两个大写字母
        Date date = new Date();//获得当前时间
        String birthday = df.format(date);
        orderUser.setCreateTime(birthday);
        flag = orderUserMapper.addOrderUser(orderUser);
        for (int i = 0 ; i < orderUser.getOrderGoods().size() ; i++) {
             orderGoods = orderUser.getOrderGoods().get(i);
             orderGoods.setOrderId(orderUser.getOrderId());
             flag = orderGoodsMapper.addOrderGoods(orderGoods);
        }
        return flag;
    }

    /**
     * 用户获取个人订单
     * */
    @Override
    public OrderUserPageByStatus getOrderUserByOpenId(String openId) {
        OrderUserPageByStatus orderUserPageByStatus = new OrderUserPageByStatus();
        List<OrderUser> list = orderUserMapper.getOrderUserById(openId);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setOrderGoods(orderGoodsMapper.getOrderGoodsByOrderId(list.get(i).getOrderId()));
        }
        orderUserPageByStatus.setAll(list);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getStatus().equals("0")) {
                orderUserPageByStatus.getStatusZero().add(list.get(i));
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getStatus().equals("1")) {
                orderUserPageByStatus.getStatusOne().add(list.get(i));
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getStatus().equals("2")) {
                orderUserPageByStatus.getStatusTwo().add(list.get(i));
            }
        }
        return orderUserPageByStatus;
    }

    @Override
    public boolean deleteOrderByOrderId(Integer orderId) {
        boolean flag = false;
        flag = orderGoodsMapper.deleteOrderByOrderId(orderId);
        flag = orderUserMapper.deleteOrderByOrderId(orderId);
        return flag;
    }

    /**
     * 按条件获取全部订单
     * */
    @Override
    public PageBean<OrderUser> listByReason(Integer pageSize, Integer page, String orderIdBegin,
                                            String orderIdEnd, String status,String phone, String username,
                                            String beginTime, String endTime) {
        if (status!=null) {
            if (status.equals("待发货")) {
                status = "0";
            } else if (status.equals("已发货"))
                status = "1";
            else if (status.equals("已收货"))
                status = "2";
        }
        PageHelper.startPage(page,pageSize);
        List<OrderUser> list = orderUserMapper.listByReason(orderIdBegin,orderIdEnd,username,status,
                phone,beginTime,endTime);

        Page<OrderUser> p = (Page<OrderUser>) list;

        long total = p.getTotal();
        list = p.getResult();


        for (int i = 0; i < list.size(); i++) {
            list.get(i).setOrderGoods(orderGoodsMapper.getOrderGoodsByOrderId(list.get(i).getOrderId()));
        }
        return new PageBean<OrderUser>(total,list);
    }

    /**
     * 订单发货
     * */
    @Override
    public boolean orderSend(String orderId) {
        if (orderUserMapper.getOrderUserStatusByOpenId(orderId).equals("0"))
            return orderUserMapper.orderSend(orderId);
        else return false;
    }

    /**
     * 用户取消订单
     * */
    @Override
    public boolean cancelOrder(String orderId) {
        return this.deleteOrderByOrderId(Integer.valueOf(orderId));
    }

    /**
     * 用户确认订单
     * */

    @Override
    public boolean orderComplete(String orderId) {
        return orderUserMapper.orderComplete(orderId);
    }
}
